package org.zoomdev.zoom.rpc.client;

import org.apache.commons.lang3.StringUtils;

import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.util.Map;

public class RpcService {

    final String ip;

    final int port;

    private int h;

    @Override
    public int hashCode() {
        int h = this.h;
        if (h == 0) {
            h = this.h = ip.hashCode() + port;
        }
        return h;
    }

    @Override
    public boolean equals(Object obj) {
        if (!(obj instanceof RpcService)) {
            return false;
        }

        RpcService key = (RpcService) obj;
        if (!StringUtils.equals(ip, key.ip)) {
            return false;
        }

        return port == key.port;
    }


    Map<String, Integer> ids;
    private SocketAddress address;

    public RpcService(String ip, int port) {
        this.ip = ip;
        this.port = port;
    }

    public SocketAddress toAddress() {
        if (address == null) {
            address = new InetSocketAddress(ip, port);
        }
        return address;
    }

    public RpcService setIds(Map<String, Integer> ids) {
        this.ids = ids;
        return this;
    }


}
